package insight.sm.controllers;

import insight.sm.delegates.AjaxDelegate;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController extends GenericController {
	@RequestMapping("/home.htm")
	public ModelAndView hello(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("serverTime", "10:00pm");
		return mav;
	}
	
	@RequestMapping("/grid.htm")	
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("serverTime", "10:00pm");
		return new ModelAndView();
	}
	@RequestMapping("/project.htm")
	public ModelAndView project(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("project");
		mav.addObject("serverTime", "10:00pm");
		return new ModelAndView();
	}
	@RequestMapping("/users.htm")
	public ModelAndView listUsers(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("serverTime", "10:00pm");
		return new ModelAndView();
	}
	@RequestMapping("/home1.htm")
	public ModelAndView home1(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mav=new ModelAndView("home1");	
		mav.addObject("serverTime", "10:00pm");
		
		return mav;
	}

	@RequestMapping("loadUsrAvatar.htm")
	public ModelAndView loadUserAvatar(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		InputStream readImg = new FileInputStream(
				"F:/Workspaces/Spring4/SiteMan/WebContent/resources/app/images/avatar1.jpg");
		AjaxDelegate.setResponse(readImg, response, "image/jpg");
		return null;
	}

	@RequestMapping("getList.htm")
	public ModelAndView getList(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("id") String id)
			throws IOException, JSONException {
		JSONArray jAry = new JSONArray();
		jAry.put(new JSONObject().put("value", "1").put("text", "One"));
		AjaxDelegate.setResponse(jAry, response);
		return null;

	}

	@RequestMapping("loadBookmarks.htm")
	public ModelAndView loadBookmarks(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("topOnly") boolean topOnly) throws IOException,
			JSONException {

		JSONArray jAry = new JSONArray();
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket")
				.put("title", "SB-1000, Right to Way not available in Zone 2")
				.put("text", "QI-01811, Right to way..."));
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket")
				.put("title", "SB-1000, Right to Way not available in Zone 2")
				.put("text", "QI-01811, Right to way..."));
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket")
				.put("title", "SB-1000, Right to Way not available in Zone 2")
				.put("text", "QI-01811, Right to way..."));
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket")
				.put("title", "SB-1000, Right to Way not available in Zone 2")
				.put("text", "QI-01811, Right to way..."));
		AjaxDelegate.setResponse(jAry, response);
		return null;

	}

	@RequestMapping("loadReports.htm")
	public ModelAndView loadReports(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("topOnly") boolean topOnly) throws IOException,
			JSONException {

		JSONArray jAry = new JSONArray();
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket").put("title", "Report1")
				.put("text", "Report1"));
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket").put("title", "Report 2")
				.put("text", "Report 2"));
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket").put("title", "Report 3")
				.put("text", "Report 3"));
		jAry.put(new JSONObject().put("type", "1").put("id", 2)
				.put("iconClass", "fa-ticket").put("title", "Report 4")
				.put("text", "Report 4"));
		AjaxDelegate.setResponse(jAry, response);
		return null;

	}

	@RequestMapping("loadProjectNavigator.htm")
	public ModelAndView loadProjectNavigator(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("topOnly") boolean topOnly) throws IOException,
			JSONException {

		JSONArray jAry = new JSONArray();
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Create New")
				.put("text", "Create New"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Tasks")
				.put("text", "Tasks"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Pictures")
				.put("text", "Pictures"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Messages")
				.put("text", "Messages"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Transmittals")
				.put("text", "Transmittals"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Submittals")
				.put("text", "Submittals"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Issues")
				.put("text", "Issues"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Risks")
				.put("text", "Risks"));
		jAry.put(new JSONObject().put("type", "1")
				.put("iconClass", "fa-ticket").put("title", "Safety Incidents")
				.put("text", "Safety Incidents"));
		AjaxDelegate.setResponse(jAry, response);
		return null;

	}

	@RequestMapping("loadBullitenBoard.htm")
	public ModelAndView loadBullitenBoard(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("topOnly") boolean topOnly) throws IOException,
			JSONException {

		JSONArray jAry = new JSONArray();
		jAry.put(new JSONObject()
				.put("type", "1")
				.put("id", 2)
				.put("avatarClass", "avatar blue")
				.put("iconClass", "fa fa-rss fa-2x")
				.put("date", "06-Jan")
				.put("title", "Twitter Bootstrap v3.0 is coming!")
				.put("text",
						"With 2.2.2 out the door, our attention has shifted almost entirely to the next major update to the project ..."));
		jAry.put(new JSONObject()
				.put("type", "1")
				.put("id", 2)
				.put("avatarClass", "avatar green")
				.put("iconClass", "fa fa-comment-o fa-2x")
				.put("date", "11-Feb")
				.put("title", "Ruby on Rails 4.0")
				.put("text",
						"Rails 4.0 is still unfinished, but it is shaping up to become a great release ..."));
		jAry.put(new JSONObject()
				.put("type", "1")
				.put("id", 2)
				.put("avatarClass", "avatar purple")
				.put("date", "04-Mar")
				.put("iconClass", "fa fa-calendar-o fa-2x")
				.put("title", "All about SCSS")
				.put("text",
						"Sass makes CSS fun again. Sass is an extension of CSS3, adding nested rules ..."));
		
		AjaxDelegate.setResponse(jAry, response);
		return null;

	}
}
